Skip to content

fix(astro): use prerender loader environment for metadata - #16618

Closed
Crystora wants to merge 1 commit into
withastro:mainfrom
Crystora:fix/cloudflare-node-prerender-dev-styles
Closed

Crystora wants to merge 1 commit into
withastro:mainfrom
Crystora:fix/cloudflare-node-prerender-dev-styles

Conversation

@Crystora

@Crystora Crystora commented May 6, 2026

Copy link
Copy Markdown
Contributor

Changes

  • Fixes ModuleLoader.getSSREnvironment() to return the ssrEnvironment parameter the loader was constructed with, instead of always looking up viteServer.environments.ssr.
  • With adapters that register a separate prerender Vite dev environment (notably @astrojs/cloudflare with prerenderEnvironment: 'node'), createViteLoader builds a dedicated loader per environment. Every other method already uses the constructor parameter - only getSSREnvironment() ignored it, so the prerender handler's loader returned the unrelated ssr environment and getComponentMetadata() walked the wrong module graph in dev. With Cloudflare's default workerd ssr, that env is also non-runnable, making the as RunnableDevEnvironment cast unsafe.
  • Removes the now-unused ASTRO_VITE_ENVIRONMENT_NAMES import.
  • Changeset: astro: patch (.changeset/fix-module-loader-prerender-env.md).
  • Closes @astrojs/cloudflare: prerenderEnvironment: 'node' swallows scoped <style> blocks into <template> in dev #16436. Follow-up to fix: propagate head metadata across ssr and prerender envs in dev #16292, which fixed the visible style-into-<template> symptom in astro:head-metadata but did not touch this separate correctness bug in the per-environment module loader.

Testing

  • tsc --noEmit on packages/astro passes cleanly.
  • Existing regression test packages/astro/test/head-propagation-prerender-env.test.js still passes (1/1) - confirms no regression on the astro:head-metadata path that fix: propagate head metadata across ssr and prerender envs in dev #16292 fixed.
  • No new test added: loader.getSSREnvironment() has a single caller (getComponentMetadata), and when only the ssr env exists ssrEnvironment === viteServer.environments.ssr, so the new behaviour is a strict superset of the old correct behaviour. Happy to extend the head-propagation-prerender-env fixture with a prerendered route to exercise the prerender handler's loader directly if maintainers prefer.

Docs

No docs change needed - this is a dev-server-internal correctness fix with no user-facing API or behaviour change. Users on adapters with a separate prerender env (e.g. @astrojs/cloudflare with prerenderEnvironment: 'node') will simply see correct head metadata for prerendered routes in dev.

@changeset-bot

changeset-bot Bot commented May 6, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: c83c987

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions Bot added the pkg: astro Related to the core `astro` package (scope) label May 6, 2026
@Crystora

Crystora commented May 6, 2026

Copy link
Copy Markdown
Contributor Author

Hi @ematipico @matthewp, could you please take a look at my small PR? Welcome to any feedback

@codspeed

codspeed Bot commented May 6, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 18 untouched benchmarks


Comparing bittoby:fix/cloudflare-node-prerender-dev-styles (c83c987) with main (12a03f2)

Open in CodSpeed

@fkatsuhiro fkatsuhiro left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @bittoby san

Thank you for your PR.
Could you create test for this issue to prevent the recurrence of the same one?Thank you!

@delucis

delucis commented Jun 12, 2026

Copy link
Copy Markdown
Member

Thanks for the PR @bittoby! It looks like the linked issues are already fixed, so I guess we can close this. Please feel free to leave a comment if I misunderstood and we should reopen!

@delucis delucis closed this Jun 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: astro Related to the core `astro` package (scope)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@astrojs/cloudflare: prerenderEnvironment: 'node' swallows scoped <style> blocks into <template> in dev

3 participants